package com.pinyougou.manager.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class LoginController {
    //用户登录
    @PostMapping("/login")
    public String login(String username,String password){
        try {
            //获取认证主体
            Subject subject = SecurityUtils.getSubject();
            //创建用户名与密码令牌
            UsernamePasswordToken token= new  UsernamePasswordToken(username,password);
            //身份认证
            subject.login(token);
            //判断用户是否通过身份验证
            if (subject.isAuthenticated()){
                return "redirect:/admin/index.html";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/login.html";

    }
    //获取登录用户名
    @GetMapping("/findLoginName")
    @ResponseBody
    public Map<String,String> findLoginName(){
        //获取登录用户名
        String loginName = SecurityUtils.getSubject().getPrincipal().toString();
        Map<String,String>data= new HashMap<>();
        data.put("loginName",loginName);
        return data;
    }


}















